home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Sprite 1984 - 1993
/
Sprite 1984 - 1993.iso
/
src
/
lib
/
c
/
stdio
/
RCS
/
fseek.c,v
< prev
next >
Wrap
Text File
|
1991-12-02
|
6KB
|
239 lines
head 1.5;
branch ;
access ;
symbols sprited:1.5.1;
locks ; strict;
comment @ * @;
1.5
date 89.06.15.22.37.53; author douglis; state Exp;
branches 1.5.1.1;
next 1.4;
1.4
date 88.07.29.18.56.35; author ouster; state Exp;
branches ;
next 1.3;
1.3
date 88.07.25.13.12.53; author ouster; state Exp;
branches ;
next 1.2;
1.2
date 88.07.21.16.04.25; author ouster; state Exp;
branches ;
next 1.1;
1.1
date 88.06.10.16.23.51; author ouster; state Exp;
branches ;
next ;
1.5.1.1
date 91.12.02.19.58.11; author kupfer; state Exp;
branches ;
next ;
desc
@@
1.5
log
@Optimize for the case in which we are doing only reads and we
can reset the pointers without doing a real lseek. (Don't
bother if relative to EOF, or if the buffer is invalid.) This
is useful when people want to peek forward more than one
character at a time and use fseek to reset the buffer after
peeking.
@
text
@/*
* fseek.c --
*
* Source code for the "fseek" library procedure.
*
* Copyright 1988 Regents of the University of California
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies. The University of California
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
*/
#ifndef lint
static char rcsid[] = "$Header: /sprite/src/lib/c/stdio/RCS/fseek.c,v 1.4 88/07/29 18:56:35 ouster Exp Locker: douglis $ SPRITE (Berkeley)";
#endif not lint
#include "stdio.h"
#include "fileInt.h"
extern long lseek();
/*
*----------------------------------------------------------------------
*
* fseek --
*
* Modify the access position of a stream.
*
* Results:
* Returns 0 if the seek was completed successfully, -1 if any
* sort of error occurred.
*
* Side effects:
* The access position of stream (i.e. the place in the file where
* the next character will be read or written) is set to the sum
* of offset and a base value. If base is 0, the base value is 0.
* If base is 1, the base value is the current access position.
* If base is 2, the base value is the length of the file.
*
*----------------------------------------------------------------------
*/
long
fseek(stream, offset, base)
register FILE *stream; /* Stream whose position is to
* be changed. */
int offset; /* See above for explanation. */
int base; /* See above for explanation. */
{
int result;
if ((stream->readProc != (void (*)()) StdioFileReadProc)
|| ((stream->flags & (STDIO_READ|STDIO_WRITE)) == 0)) {
return -1;
}
/*
* Optimize for the case in which we are doing only reads and we
* can reset the pointers without doing a real lseek. (Don't
* bother if relative to EOF, or if the buffer is invalid.) This
* is useful when people want to peek forward more than one
* character at a time and use fseek to reset the buffer after
* peeking.
*/
if (((stream->flags & (STDIO_READ|STDIO_WRITE)) == STDIO_READ) &&
(base != 2) && stream->readCount > 0) {
int endAddr; /* file pointer for end of read buffer */
int curAddr; /* file pointer for current ptr into read
buffer */
int startAddr; /* file pointer for start of read buffer */
int newAddr; /* file pointer after seek */
endAddr = lseek((int) stream->clientData, (long) 0, 1);
if (endAddr == -1) {
return -1;
}
curAddr = endAddr - stream->readCount;
startAddr = curAddr - (stream->lastAccess + 1 - stream->buffer);
newAddr = offset;
if (base == 1) {
newAddr += curAddr;
}
if (newAddr >= startAddr && newAddr <= endAddr) {
stream->readCount += curAddr - newAddr;
stream->lastAccess -= curAddr - newAddr;
stream->flags &= ~STDIO_EOF;
return 0;
}
}
/*
* I'm going to reset all the buffer pointers, so flush any pending
* output.
*/
result = fflush(stream);
/*
* Compute the offset and base to pass to the system to reposition.
* This is a tricky if the base value is the current access position:
* have to account for the characters that the system has passed to
* me but that I haven't passed to the user.
*/
if (base == 1) {
offset -= stream->readCount;
}
stream->readCount = 0;
stream->writeCount = 0;
stream->lastAccess = stream->buffer - 1;
stream->flags &= ~STDIO_EOF;
if (lseek((int) stream->clientData, (long) offset, base) == -1) {
return -1;
}
return result;
}
@
1.5.1.1
log
@Initial branch for Sprite server.
@
text
@d17 1
a17 1
static char rcsid[] = "$Header: /sprite/src/lib/c/stdio/RCS/fseek.c,v 1.5 89/06/15 22:37:53 douglis Exp $ SPRITE (Berkeley)";
@
1.4
log
@Lint.
@
text
@d17 1
a17 1
static char rcsid[] = "$Header: fseek.c,v 1.3 88/07/25 13:12:53 ouster Exp $ SPRITE (Berkeley)";
d60 34
@
1.3
log
@Lint.
@
text
@d17 1
a17 1
static char rcsid[] = "$Header: fseek.c,v 1.2 88/07/21 16:04:25 ouster Exp $ SPRITE (Berkeley)";
d83 1
a83 1
if (lseek((int) stream->clientData, offset, base) == -1) {
@
1.2
log
@Fseek must return a "long".
@
text
@d17 1
a17 1
static char rcsid[] = "$Header: fseek.c,v 1.1 88/06/10 16:23:51 ouster Exp $ SPRITE (Berkeley)";
d22 2
@
1.1
log
@Initial revision
@
text
@d17 1
a17 1
static char rcsid[] = "$Header: atoi.c,v 1.1 88/04/28 17:20:23 ouster Exp $ SPRITE (Berkeley)";
d44 1
a44 1
int
@